package cc.sunwl.study.springboot2.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("demo")
public class DemoController {
    @GetMapping(value = "hello", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Mono<String> hello(String name) {
        return Mono.create(e ->
                e.success("你好, " + name)
        );
    }

    @GetMapping(value = "hi/{name}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Flux<List<String>> hi(@PathVariable String name) {
        return Flux.create(e -> {
                    List<String> stringList1 = new ArrayList<>();
                    stringList1.add("嗨 " + name + ", 你好");
                    List<String> stringList2 = new ArrayList<>();
                    stringList2.add("嗯 " + name + ", 下一位");
                    e.next(stringList1).next(stringList2).complete();
                }
        );
    }

    public Mono<ServerResponse> echo(ServerRequest request) {
        return ServerResponse.ok().body(request.bodyToMono(String.class), String.class);
    }

}
